package Interview;

import java.util.Stack;

/**
 * 大数相加
 *
 * @author: leiwenkang02@meituan.com
 * @date: 2018/4/12
 * @time: 12:41
 */
public class Test01大数相加 {
    public static void main(String[] args) {
        int[] array1 = {1, 8, 6, 5, 4, 2, 7, 8, 7};
        int[] array2 = {9, 1, 5, 6, 3, 2};
        int[] result = addBigeNum(array1, array2);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i]);
        }
        System.out.println();
        System.out.print(186542787 + 915632);
    }

    public static int[] addBigeNum(int[] array1, int[] array2) {
        if (array1 == null || array1.length == 0) {
            return array2;
        }
        if (array2 == null || array2.length == 0) {
            return array1;
        }
        Stack<Integer> stack = new Stack<>();
        int index1 = array1.length - 1;
        int index2 = array2.length - 1;
        int takeOver = 0;
        while (index1 >= 0 || index2 >= 0) {
            int num1 = 0;
            if (index1 >= 0) {
                num1 = array1[index1];
            }
            int num2 = 0;
            if (index2 >= 0) {
                num2 = array2[index2];
            }
            int sum = num1 + num2 + takeOver;
            if (sum >= 10) {
                sum -= 10;
                takeOver = 1;
            } else {
                takeOver = 0;
            }
            stack.push(sum);
            index1--;
            index2--;
        }

        int[] result = new int[stack.size()];
        int i = 0;
        while (!stack.isEmpty()) {
            result[i++] = stack.pop();
        }
        return result;

    }

}
